package com.it.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.it.admin.service.AdminStatisticsService;
import com.it.common.domain.dos.ArticleCategoryRelDO;
import com.it.common.domain.dos.ArticleTagRelDO;
import com.it.common.domain.dos.CategoryDO;
import com.it.common.domain.dos.TagDO;
import com.it.common.mapper.ArticleCategoryRelMapper;
import com.it.common.mapper.ArticleTagRelMapper;
import com.it.common.mapper.CategoryMapper;
import com.it.common.mapper.TagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdminStatisticsServiceImpl implements AdminStatisticsService {

    @Resource
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private TagMapper tagMapper;

    @Resource
    private ArticleTagRelMapper articleTagRelMapper;

    @Override
    public void statisticsCategoryArticleTotal() {
        // 查询所有分类
        List<CategoryDO> categoryDOS = categoryMapper.findCategorySelectList();
        // 查询所有文章-分类映射记录
        List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelMapper.selectList(Wrappers.emptyWrapper());
        // 按所属分类 ID 进行分组
        Map<Long,List<ArticleCategoryRelDO>> articleCategoryMap = Maps.newHashMap();;
        if(!CollectionUtils.isEmpty(articleCategoryRelDOS)){
            articleCategoryMap = articleCategoryRelDOS.stream()
                    .collect(Collectors.groupingBy(ArticleCategoryRelDO::getCategoryId));
        }

        if(!CollectionUtils.isEmpty(categoryDOS)){
            // 循环统计各分类下的文章总数
           for(CategoryDO categoryDO : categoryDOS){
               Long categoryId = categoryDO.getId();
               List<ArticleCategoryRelDO> articleCategoryRelDOList = articleCategoryMap.get(categoryId);
               // 获取文章总数
               int articlesTotal = CollectionUtils.isEmpty(articleCategoryRelDOList) ? 0 : articleCategoryRelDOList.size();
               // 更新该分类的文章总数
               CategoryDO categoryDO1 = CategoryDO.builder()
                       .id(categoryId).articlesTotal(articlesTotal).build();
               categoryMapper.updateById(categoryDO1);
           }
        }
    }

    @Override
    public void statisticsTagArticleTotal() {
        //查询所有分类
        List<TagDO> tagDOS = tagMapper.findAll();
        //查询所有文章-标签映射记录
        List<ArticleTagRelDO> articleTagRelDOS = articleTagRelMapper.selectList(Wrappers.emptyWrapper());
        // 按所属分类 ID 进行分组
        Map<Long,List<ArticleTagRelDO>> articleTagMap = Maps.newHashMap();
        if(!CollectionUtils.isEmpty(articleTagRelDOS)){
            articleTagMap = articleTagRelDOS.stream().collect(Collectors.groupingBy(ArticleTagRelDO::getTagId));
        }
        if(!CollectionUtils.isEmpty(tagDOS)){
            for(TagDO tagDO : tagDOS){
                Long tagId = tagDO.getId();
                // 获取此标签下所有映射记录
                List<ArticleTagRelDO> articleTagRelDOS1 = articleTagMap.get(tagId);
                // 获取文章总数
                int articlesTotal = CollectionUtils.isEmpty(articleTagRelDOS1)?0: articleTagRelDOS1.size();
                TagDO tagDO1 = TagDO.builder()
                        .id(tagId).articlesTotal(articlesTotal).build();

                tagMapper.updateById(tagDO1);
            }
        }
    }
}
